package com.scada.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scada.dao.IMemberBuyServiceDao;
import com.scada.inf.MemberBuyServiceVisitor;
import com.scada.model.bean.MemberBuyService;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.MemberBuyServiceSearch;

@Service("memberBuyServiceVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class MemberBuyServiceVisitorImpl implements MemberBuyServiceVisitor{

	@Autowired
	private IMemberBuyServiceDao memberBuyServiceDao;

	@Transactional
	@Override
	public void deleteMemberBuyServiceById(String isShow, String ids) {
		
		try {
			Map<String,Object> map = null;
			for(String id : ids.split(",")) {
				map = new HashMap<String,Object>(); 
				map.put("isShow", isShow);
				map.put("id", Long.parseLong(id));
				memberBuyServiceDao.deleteMemberBuyServiceById(map);
			}
		}catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@Transactional
	@Override
	public void insertMemberBuyService(MemberBuyService memberBuyService) {
		
		try{
			memberBuyServiceDao.insertMemberBuyService(memberBuyService);
			Map<String,Object> map = new HashMap<String,Object>(); 
			map.put("ifExpire", MemberBuyService.ifExpireCode.YES.getKey());
			map.put("memberId", memberBuyService.getMemberId());
			map.put("serviceId", memberBuyService.getServiceId());
			map.put("buyStartDate", memberBuyService.getBuyStartDate());
			memberBuyServiceDao.updateIfExpireByMember(map);
		}catch(Exception e){
			throw new RuntimeException();
		}
	}

	@Override
	public ResponseBean getMemberBuyServiceById(Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			responseBean.addObject("result", memberBuyServiceDao.getMemberBuyServiceById(id));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Transactional
	@Override
	public void updateMemberBuyService(MemberBuyService memberBuyService) {
		
		try{
			memberBuyServiceDao.updateMemberBuyService(memberBuyService);
			Map<String,Object> map = new HashMap<String,Object>(); 
			map.put("ifExpire", MemberBuyService.ifExpireCode.YES.getKey());
			map.put("memberId", memberBuyService.getMemberId());
			map.put("serviceId", memberBuyService.getServiceId());
			map.put("buyStartDate", memberBuyService.getBuyStartDate());
			memberBuyServiceDao.updateIfExpireByMember(map);
		}catch(Exception e){
			throw new RuntimeException();
		}
	}

	@Override
	public ResponseBean getMemberBuyServicePageData(MemberBuyServiceSearch memberBuyServiceSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map.put("isShow", MemberBuyService.IsShowCode.YES.getKey());
			map.put("serviceId", memberBuyServiceSearch.getServiceId());
			map.put("memberId", memberBuyServiceSearch.getMemberId());
			map.put("serviceType", memberBuyServiceSearch.getServiceType());
			map.put("investmentType", memberBuyServiceSearch.getInvestmentType());
			map.put("buyStartDate", memberBuyServiceSearch.getBuyStartDate());
			map.put("buyEndDate", memberBuyServiceSearch.getBuyEndDate());
			responseBean.addObject("resultCount", memberBuyServiceDao.getMemberBuyServicePageCount(map));
			map.put("start", memberBuyServiceSearch.getStart());
			map.put("pageSize", memberBuyServiceSearch.getLength());
            responseBean.addObject("resultList", memberBuyServiceDao.getMemberBuyServicePageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean validateBuyRepeat(Long id, String buyStartDate, String buyEndDate, Long memberId,
			Long serviceId) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map.put("isShow", MemberBuyService.IsShowCode.YES.getKey());
			map.put("serviceId", serviceId);
			map.put("memberId", memberId);
			map.put("id", id);
			map.put("buyStartDate", buyStartDate);
			map.put("buyEndDate", buyEndDate);
            responseBean.addObject("result", memberBuyServiceDao.validateBuyRepeat(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}
	
	@Override
	public ResponseBean updateIfRenew(String ifRenew, Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			Map<String,Object> map = new HashMap<String,Object>(); 
			map.put("ifRenew", ifRenew);
			map.put("id", id);
			memberBuyServiceDao.updateIfRenew(map);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}
}
